package com.ericsson.eritask.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ericsson.eritask.dao.ProjectDAO;
import com.ericsson.eritask.domain.Project;
import com.ericsson.eritask.domain.User;
import com.ericsson.eritask.domain.UserProject;
import com.ericsson.eritask.dto.ProjectDTO;


@Repository
public class ProjectDaoImpl implements ProjectDAO {

	
	@PersistenceContext
	 EntityManager en;
	

	
	@Transactional
	public List<String> getAllProjects(){
		 List<Project> projects=null;
	     List<String> list =null;
		
		System.out.println("getAllProjects()....");
		//String query= ;
		//if(en.createQuery("select p from Project p",Project.class)!=null){
			System.out.println("en"+en);
		TypedQuery<Project> qr = en.createQuery("select p from Project p",Project.class);
		
		// qr = en.createNamedQuery(query, Project.class);
		
		projects = qr.getResultList( );

		
		list = new ArrayList<String>();
		//projects= new ArrayList<Project>();
		for( Project p:projects ) {
		 
			System.out.println("\t Project Name :" + p.getProjectName());
			String project_name = p.getProjectName();
			//int project_Id = p.getProjectId();
			//projects.add(p);
			list.add(project_name);
		 }
			if(projects!=null){
				System.out.println("list size"+projects.size());
			}
		//}

	/*}else {
		System.out.println("Nullpointer get all projects");
	}*/
		
		System.out.println(" List found"+list);
		return list;
	}



	@Transactional
	@Override
	public List<ProjectDTO> getAllProjectsOfUser(String signum) {


		
		//List<String> list=new ArrayList<String>();
		List<ProjectDTO> list=new ArrayList<ProjectDTO>();
		//List<User> userList = null;
		List<UserProject> userProjectList = null;
		
		try{

			User u =en.find(User.class, signum);
		Query query = en.createQuery("select u from UserProject u where u.user.signum = ?1 ",UserProject.class );
		query.setParameter(1,u.getSignum());
		userProjectList = query.getResultList();
		System.out.println("userProjectList:"+userProjectList.size());
		for(UserProject  userproject:userProjectList ){
			ProjectDTO prdto = new ProjectDTO();
			System.out.println("userproject id :::::::::"+userproject.getProject().getProjectId());
			System.out.println("userproject name :::::::::"+userproject.getProject().getProjectName());
			//String projectName=userproject.getProject().getProjectName();
			//int projectID=userproject.getProject().getProjectId();
			prdto.setProjectId(userproject.getProject().getProjectId());
			prdto.setProjectName(userproject.getProject().getProjectName());
			list.add(prdto);
			//list.add(projectName);
		}
	  System.out.println("List size........"+list.size()); 
	  
	}
	   catch(Exception e){
		   e.printStackTrace();
	   }
		   
		return list;
	

		
		
	}
	
	/*@Transactional
	public List<Project> findAllProjects() {
	
	//return (List<Project>) en.find(Project.class, "projectId");
		return null;
	
	}*/
}